package com.example.cigarettepricesystem;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class UpdatePriceActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView ivCImg;
    private EditText etCigName;
    private EditText etCigShell;
    private EditText etCigPrice;
    private Button btnCigUpdate;
    private Button btnCigDelete;
    private Context context = UpdatePriceActivity.this;
    private SQLiteDatabase db;
    private String cname,cprice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_price);
        Intent intent = getIntent();
        cname = intent.getStringExtra("cname");
        cprice = intent.getStringExtra("cprice");
        init();
        show();
    }

    private void init() {
        ivCImg = (ImageView) findViewById(R.id.iv_cImg);
        etCigName = (EditText) findViewById(R.id.et_cigName);
        etCigShell = (EditText) findViewById(R.id.et_cigShell);
        etCigPrice = (EditText) findViewById(R.id.et_cigPrice);
        btnCigUpdate = (Button) findViewById(R.id.btn_cigUpdate);
        btnCigDelete = (Button) findViewById(R.id.btn_cigDelete);
        btnCigUpdate.setOnClickListener(this);
        btnCigDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_cigUpdate:
                String cigName,cigPrice,cigShell;
                cigName = etCigName.getText().toString().trim();
                cigPrice = etCigPrice.getText().toString().trim();
                cigShell = etCigShell.getText().toString().trim();
                if (cigName.isEmpty()){
                    Toast.makeText(context, "请输入香烟名称！", Toast.LENGTH_SHORT).show();
                }else if (cigShell.isEmpty()){
                    Toast.makeText(context, "请输入硬软壳！", Toast.LENGTH_SHORT).show();
                }else if (cigPrice.isEmpty()){
                    Toast.makeText(context, "请输入香烟价格！", Toast.LENGTH_SHORT).show();
                }else if (cigShell.equals("硬壳") == false && cigShell.equals("软壳") == false){
                    Toast.makeText(context, "只能输入硬壳或软壳！", Toast.LENGTH_SHORT).show();
                }else {
                    //设置对话框
                    AlertDialog dialog;
                    AlertDialog.Builder builder = new AlertDialog.Builder(this)
                            .setTitle("更改记录")
                            .setMessage("是否更改此香烟记录？")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    db = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);//获取可读写数据库对象
                                    ContentValues values = new ContentValues();
                                    values.put("cigaretteName",cigName);
                                    values.put("cigarettePrice",cigPrice);
                                    values.put("cigaretteShell",cigShell);
                                    db.update("tb_cigarette",values,
                                            "cigaretteName=? AND cigarettePrice=?",
                                            new String[]{cname,cprice});
                                    Toast.makeText(context, "修改成功！", Toast.LENGTH_SHORT).show();
                                    db.close();
                                    finish();
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    dialog = builder.create();
                    dialog.show();
                }
                break;
            case R.id.btn_cigDelete:
                //设置对话框
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(this)
                        .setTitle("删除记录")
                        .setMessage("是否删除此香烟？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                db = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);//获取可读写数据库对象
                                db.delete("tb_cigarette", "cigaretteName=? AND cigarettePrice=?",
                                        new String[]{cname,cprice});
                                Toast.makeText(context, "删除成功！", Toast.LENGTH_SHORT).show();
                                db.close();
                                finish();
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog = builder.create();
                dialog.show();
                break;
        }
    }

    //将数据库中的数据显示到页面上
    @SuppressLint("Range")
    private void show() {
        db = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);//获取可读写数据库对象
        Cursor cursor = db.query("tb_cigarette",null,
                "cigaretteName=? AND cigarettePrice=?",
                new String[]{cname,cprice},
                null,null,null);    //根据输入的账号查询
        cursor.moveToFirst();
        String cName = cursor.getString(cursor.getColumnIndex("cigaretteName"));
        String cPrice = cursor.getString(cursor.getColumnIndex("cigarettePrice"));
        String cShell = cursor.getString(cursor.getColumnIndex("cigaretteShell"));
        byte[] img = cursor.getBlob(cursor.getColumnIndex("cigaretteImg"));
        //将byte[]数组里的数据转化为位图并显示到imageview中
        Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
        ivCImg.setImageBitmap(bitmap);
        etCigName.setText(cName);
        etCigShell.setText(cShell);
        etCigPrice.setText(cPrice);
        cursor.close();
        db.close();
    }
}